<?php

/**
 * search_configuration actions.
 *
 * @package    directory
 * @subpackage search_configuration
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class search_configurationActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  	
  	$this->form = new SearchConfigurationForm();
  	
  	//$this->master_directories = $this->getActiveDirectoriesWithTemplates();
  	//$this->master_directories = $this->filterCategoryDirectories();
  	
  	
  	
    //$this->selected_directory = $this->getUser()->getAttribute("selected_directory") == null ? $this->master_directories[0]->getId() : $this->getUser()->getAttribute("selected_directory");
    
  	$this->selected_directory = $this->getUser()->getAttribute("directory_id");
  	
  	$this->directory = Doctrine_Core::getTable('MasterDirectory')->find($this->selected_directory);
  	
    //$this->selected_directory = $request->getParameter("directory_id");
    
    
    
    //$this->templates = Doctrine_Core::getTable('Template')->retreiveActiveTemplates();
    $this->templates = Doctrine_Core::getTable('Template')->retreiveActiveTemplatesByDirectoryWithoutHome($this->selected_directory);
    
    $all_categories = Doctrine_Core::getTable('Category')->retreiveActiveCategoriesByDirectory($this->selected_directory);

    $this->categories = $this->getCategoriesWithLevels($all_categories);
    
   
//    $this->selected_template_id = $this->templates[0]->getId(); //count($this->templates[0]->getMasterDirectory()) == 0 ? $this->templates[0]->getId() : $this->templates[1]->getId();

    $existing_directory_configuration = Doctrine_Core::getTable ( 'SearchConfiguration' )->getByDirectory($this->selected_directory );
    
    $this->selected_template_id =  $existing_directory_configuration[0]->getTemplateId();
    
    $templatesToLevel = Doctrine_Core::getTable('Template2Level')->getTemplatesLevelsByTemplateId($this->selected_template_id);
    
    $levelIds =  array();   
    
    foreach ($templatesToLevel as $template2level){
    	array_push($levelIds, $template2level->getLevelId());
    }    
    
    
    $this->levelIds =  $levelIds;	
    
    $this->inclusive_categories = array();
    $existing_configurations = Doctrine_Core::getTable ( 'SearchConfiguration' )->getByDirectoryAndTemplate( $this->selected_directory, $this->selected_template_id);
    
    foreach ($existing_configurations as $existing_configuration){
    	if ($existing_configuration->getInclusive() == 1){
    		array_push($this->inclusive_categories, $existing_configuration->getCategoryId());
    	}
    }
    
  }
  
  public function executeLoadTemplateData(sfWebRequest $request){
  	
  	$directory_id = $request->getParameter('directory_id');
  	
  	$form = new TemplateForm();
  	
  	$master_directories = $this->getActiveDirectoriesWithTemplates();
  	
    $templates = Doctrine_Core::getTable('Template')->retreiveActiveTemplatesByDirectoryWithoutHome($directory_id);
    
    $all_categories = Doctrine_Core::getTable('Category')->retreiveActiveCategoriesByDirectory($directory_id);

    $categories = $this->getCategoriesWithLevels($all_categories);
        
    $selected_directory = $directory_id;
    
    $this->getUser()->setAttribute('selected_directory',$directory_id);
    
    $existing_directory_configuration = Doctrine_Core::getTable ( 'SearchConfiguration' )->getByDirectory($selected_directory );
    
    $this->selected_template_id =  $existing_directory_configuration[0]->getTemplateId();
    
    $templatesToLevel = Doctrine_Core::getTable('Template2Level')->getTemplatesLevelsByTemplateId($this->selected_template_id);
    
    $levelIds =  array();   
    
    foreach ($templatesToLevel as $template2level){
    	array_push($levelIds, $template2level->getLevelId());
    }    
    
    $this->levelIds =  $levelIds;	    
    
    $inclusive_categories = array();
    $existing_configurations = Doctrine_Core::getTable ( 'SearchConfiguration' )->getByDirectoryAndTemplate( $directory_id, $this->selected_template_id);
    
    foreach ($existing_configurations as $existing_configuration){
    	if ($existing_configuration->getInclusive() == 1){
    		array_push($inclusive_categories, $existing_configuration->getCategoryId());
    	}
    }    
    
    
    return $this->renderPartial('template_levels_container',array('form'=>$form, 'master_directories' => $master_directories, 'selected_directory' => $directory_id, 'categories'=> $categories, 'levelIds' => $levelIds, 'templates' => $templates, 'selected_template_id' => $this->selected_template_id, 'inclusive_categories' => $inclusive_categories));
  }  
  
  
  protected function getCategoriesWithLevels($all_categories){
    $categories_with_levels = array();
    
    foreach ($all_categories as $a_category){
    	if (count($a_category->getLevel()) > 0){
    		array_push($categories_with_levels, $a_category);
    	}
    }
    return $categories_with_levels;
  }
  
  	public function getActiveDirectoriesWithTemplates(){
		$all_directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
		
		$directories_with_template = array();
		
		foreach ($all_directories as $directory){
			$templates = Doctrine_Core::getTable('Template')->retreiveActiveTemplatesByDirectory($directory->getId());
			if (count($templates) > 0 ){
				//TODO: chequear que no sea solo el template home
				if (count($templates) == 1 && $templates[0]->getId() != $directory->getHomeTemplateId() ){
					array_push($directories_with_template, $directory);
				}else if (count($templates) > 1){
					array_push($directories_with_template, $directory);
				}
			}
		}
		return $directories_with_template;  		
  	}  
    
  public function executeLoadSearchConfiguration(sfWebRequest $request){
  	  	
    $this->template = Doctrine_Core::getTable('Template')->find(array($request->getParameter('template_id')));
    
    $this->forward404Unless($this->template);

    $templatesToLevel = Doctrine_Core::getTable('Template2Level')->getTemplatesLevelsByTemplateId($this->template->getId());
    
    $home_directory = $this->template->getMasterDirectory();
    $directory_id = $templatesToLevel[0]->getLevel()->getCategory()->getMasterDirectory()->getId() != "" ? $templatesToLevel[0]->getLevel()->getCategory()->getMasterDirectory()->getId() : $home_directory[0]->getId();
    
  	$form = new TemplateForm($this->template);
  	
  	$master_directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
  	
    $templates = Doctrine_Core::getTable('Template')->retreiveActiveTemplatesByDirectory($directory_id) ;
    
    
    $all_categories = Doctrine_Core::getTable('Category')->retreiveActiveCategoriesByDirectory($directory_id);

    $categories = $this->getCategoriesWithLevels($all_categories);
    
    $selected_directory = $directory_id;
    
    $levelIds =  array();   
    
    //echo $this->template->getId() ." - ". count($templatesToLevel);
    
    foreach ($templatesToLevel as $template2level){
    	array_push($levelIds, $template2level->getLevelId());
    }
    
    $inclusive_categories = array();
    $existing_configurations = Doctrine_Core::getTable ( 'SearchConfiguration' )->getByDirectoryAndTemplate( $directory_id, $request->getParameter('template_id'));
    
    foreach ($existing_configurations as $existing_configuration){
    	if ($existing_configuration->getInclusive() == 1){
    		array_push($inclusive_categories, $existing_configuration->getCategoryId());
    	}
    }

    return $this->renderPartial('template_levels_container',array('form'=>$form, 'master_directories' => $master_directories, 'selected_directory' => $directory_id, 'categories'=> $categories, 'levelIds' => $levelIds, 'templates' => $templates, 'selected_template_id' => $request->getParameter('template_id'), 'inclusive_categories' => $inclusive_categories));
  	
  }    	
  
  public function executeCreate(sfWebRequest $request){
  	//first delete existing records for the selected directory and template

  	
  	$directory_id = $request->getParameter("directory_id");
  	$template_id = $request->getParameter("template_id");
  	
  	$existing_configurations = Doctrine_Core::getTable ( 'SearchConfiguration' )->getByDirectory($directory_id);
  	
  	foreach ($existing_configurations as $existing_configuration){
  		$existing_configuration->delete();
  	}
  	
  	$inclusive_categories = array();
  	
  	$categories = $request->getParameter("categories");
  	
  	foreach ($categories as $category){
  		$search_configuration = new SearchConfiguration();
  		$search_configuration->setDirectoryId($directory_id);
  		$search_configuration->setTemplateId($template_id);
  		$search_configuration->setCategoryId($category);
  		
  		if ($request->getParameter("chb_inclusive_" . $category)){
  			$search_configuration->setInclusive(1);
  			array_push($inclusive_categories, $category);
  		}
  		else{
  			$search_configuration->setInclusive(0);
  		}
  		
  		$search_configuration->save();
  		
  	}
  	
  	$directory =  Doctrine_Core::getTable('MasterDirectory')->find(array($directory_id));
  	
  	$directory->setSearchTitle($request->getParameter("search_title"));
  	
  	$directory->setSearchButtonCaption($request->getParameter("search_button_caption"));
  	
  	$include_in_sitemap = $request->getParameter("include_in_sitemap") != null ? 1 : 0;
  	
  	$directory->setSearchInSitemap($include_in_sitemap);
  	
  	$directory->save();
  	
    $this->template = Doctrine_Core::getTable('Template')->find(array($template_id));
    
    $this->forward404Unless($this->template);

    $templatesToLevel = Doctrine_Core::getTable('Template2Level')->getTemplatesLevelsByTemplateId($template_id);
    
    $home_directory = $this->template->getMasterDirectory();
    $directory_id = $templatesToLevel[0]->getLevel()->getCategory()->getMasterDirectory()->getId() != "" ? $templatesToLevel[0]->getLevel()->getCategory()->getMasterDirectory()->getId() : $home_directory[0]->getId();
    
  	$form = new TemplateForm($this->template);
  	
  	$master_directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
  	
    $templates = Doctrine_Core::getTable('Template')->retreiveActiveTemplatesByDirectory($directory_id) ;
    
    
    $all_categories = Doctrine_Core::getTable('Category')->retreiveActiveCategoriesByDirectory($directory_id);

    $categories = $this->getCategoriesWithLevels($all_categories);
    
    $selected_directory = $directory_id;
    
    $levelIds =  array();   
    
    //echo $this->template->getId() ." - ". count($templatesToLevel);
    
    foreach ($templatesToLevel as $template2level){
    	array_push($levelIds, $template2level->getLevelId());
    }

    $this->getUser()->setFlash('saved_flash', 'saved', false);
    
	$action_str = "Search configuration saved for directory ID: " . $directory_id;
  	DirectoryUtils::registerTrace($action_str, $this->getUser()->getAttribute("user_id"));    
    
    
    return $this->renderPartial('template_levels_container',array('form'=>$form, 'master_directories' => $master_directories, 'selected_directory' => $directory_id, 'categories'=> $categories, 'levelIds' => $levelIds, 'templates' => $templates, 'selected_template_id' => $template_id, 'inclusive_categories' => $inclusive_categories));
  	
  	
  }
  
 protected function filterCategoryDirectories(){
  	$directories_with_categories = array();
  	$levels = Doctrine_Core::getTable('Level')->loadAll();
  	foreach ($levels as $level){
  		$directory = $level->getCategory()->getMasterDirectory();
  		if($directory->getDeleted() == 0){
	  		array_push($directories_with_categories, $level->getCategory()->getMasterDirectory());
  		}
  	}
  	
  	return array_unique($directories_with_categories);
  }
  	
}
